//
//  WHStatusLiskViewModel.swift
//  WHSinaWeiBo
//
//  Created by 王寒 on 16/4/11.
//  Copyright © 2016年 wanghan. All rights reserved.
//

import UIKit

class WHStatusLiskViewModel: NSObject {

    
    
    lazy var statusArr: [WHStatusViewModel] = [WHStatusViewModel]()
    
    func loadData(finished: (isSuccess: Bool) -> ()){
        
        guard let token = WHUserAccountViewModel.sharedAccountViewModel.accountModel?.access_token else{
            print("用户未登录")
            return
        }
        
        HMNetworkTools.tool.loadHomePage(token) { (result, error) -> () in
            
            if error != nil {
                finished(isSuccess: false)
                return
            }
            
            //将result结果字典 转模型
            guard let dict = result as? [String : AnyObject] else{
                finished(isSuccess: false)
                return
            }
            ;
            //获取字典里status键 的数组
            guard let array = dict["statuses"] as? [[String : AnyObject]] else{
                finished(isSuccess: false)
                return
            }
            
            //便利字典 获取模型数组
            var tempArr = [WHStatusViewModel]()
            for item in array{
                let s = WHStatus(dict: item)
                let viewModel = WHStatusViewModel(status: s)
                tempArr.append(viewModel)
            }
            
            
            self.statusArr = tempArr
            
            finished(isSuccess: true)
            
        }
        
        
    }

}
